﻿@page
@model FytSoa.Web.Pages.FytAdmin.Sys.TaskSchedulerModel
@{
    ViewData["Title"] = "任务调度管理";
    Layout = AdminLayout.Pjax(HttpContext);
}
<div id="container">
    <style>
        .layui-btn-primary {
            border-radius: 4px !important;
            font-size: 12px;
        }
        .text-red {
            color: red !important;
        }
    </style>
    <div class="list-wall" id="app">
        <div class="layui-form list-search">
            <button type="button" class="layui-btn layui-btn-sm" data-type="toolAdd"><i class="layui-icon"></i> 新增</button>
        </div>
        <div class="layui-collapse" style="margin-top: 15px">
            <div class="layui-colla-item" v-for="(it,index) in list">
                <h2 class="layui-colla-title">{{it.groupName}}——任务组</h2>
                <div class="layui-colla-content layui-show">
                    <table class="layui-table layui-hide" lay-skin="line" :id="'table'+index" lay-filter="tool"></table>
                </div>
            </div>
        </div>
        <div v-show="list.length==0">
            <p style="padding:40px; text-align:center;">无任务</p>
        </div>

    </div>
    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                list: []
            }
        });
        layui.config({
            base: '/themes/js/modules/'
        }).use(['table', 'layer', 'jquery', 'common', 'element'],
            function () {
                var table = layui.table,
                    layer = layui.layer,
                    $ = layui.jquery,
                    os = layui.common,
                    element = layui.element;
                os.ajax('api/job/GetAllJob',
                    {},
                    function (res) {
                        if (res.statusCode == 200) {
                            vm.list = res.data;
                            console.log(vm.list);
                            vm.$nextTick(function () {
                                element.render('collapse');
                                for (var i = 0; i < res.data.length; i++) {
                                    var id = '#table' + i;
                                    table.render({
                                        elem: id,
                                        cols: [
                                            [
                                                { field: 'name', width: 120, title: '任务名称', fixed: 'left' },
                                                {
                                                    field: 'displayState',
                                                    title: '状态',
                                                    templet: function (d) {
                                                        if (d.displayState == '正常') {
                                                            return '<span class="layui-badge layui-bg-blue">正常</span>';
                                                        } else {
                                                            return '<span class="layui-badge">' +
                                                                d.displayState +
                                                                '</span>';
                                                        }
                                                    }
                                                },
                                                { field: 'requestUrl', width: 120, title: 'Url' },
                                                { field: 'lastErrMsg', title: '异常信息' },
                                                { field: 'beginTime', width: 150, title: '开始时间' },
                                                { field: 'previousFireTime', title: '上次执行时间' },
                                                { field: 'nextFireTime', title: '下次执行时间' },
                                                { field: 'interval', title: '执行计划' },
                                                { field: 'description', width: 100, title: '描述' },
                                                {
                                                    width: 200,
                                                    title: '操作',
                                                    templet: function (d) {
                                                        var str = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">暂停</a>';
                                                        str +=
                                                            '<a class="layui-btn layui-btn-primary layui-btn-xs text-red" lay-event="del">删除</a>';
                                                        str +=
                                                            '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="del">恢复执行</a>';
                                                        return str;
                                                    }
                                                }
                                            ]
                                        ],
                                        page: false,
                                        data: res.data[i].jobInfoList
                                    });
                                }
                            });
                        } else {
                            os.error(res.message);
                        }
                    },
                    'get');


                var guid = '',
                    active = {
                        reload: function () {
                            table.reload('tables',
                                {
                                    page: {
                                        curr: 1
                                    }
                                });
                        },
                        toolAdd: function () {
                            os.Open('添加任务调度',
                                '/fytadmin/sys/taskschedulermodify',
                                '500px',
                                '600px',
                                function () {
                                    active.reload();
                                });
                        },
                        toolDel: function () {
                            var checkStatus = table.checkStatus('tables'), data = checkStatus.data;
                            if (data.length === 0) {
                                os.error("请选择要删除的项目~");
                                return;
                            }
                            var str = '';
                            $.each(data,
                                function (i, item) {
                                    str += item.guid + ",";
                                });
                            layer.confirm('确定要执行批量删除吗？',
                                function (index) {
                                    layer.close(index);
                                    var loadindex = layer.load(1,
                                        {
                                            shade: [0.1, '#000']
                                        });
                                    os.ajax('api/setting/delete/',
                                        { parm: str },
                                        function (res) {
                                            layer.close(loadindex);
                                            if (res.statusCode === 200) {
                                                active.reload();
                                                os.success('删除成功！');
                                            } else {
                                                os.error(res.message);
                                            }
                                        });
                                });

                        }
                    };
                $('.list-search .layui-btn').on('click',
                    function () {
                        var type = $(this).data('type');
                        active[type] ? active[type].call(this) : '';
                    });
                //监听工具条
                table.on('tool(tool)',
                    function (obj) {
                        var data = obj.data;
                        if (obj.event === 'edit') {
                            os.Open('修改更新信息',
                                '/fytadmin/sys/taskschedulermodify/?guid=' + data.guid,
                                '650px',
                                '420px',
                                function () {
                                    active.reload();
                                })
                        }
                    });
            });
    </script>
    <script type="text/html" id="tool">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit"><i class="layui-icon"></i> 修改</a>
    </script>
</div>